from nicegui import events, ui


def handle_draw(e: events.GenericEventArguments):
    if e.args["layerType"] == "marker":
        m.marker(
            latlng=(
                e.args["layer"]["_latlng"]["lat"],
                e.args["layer"]["_latlng"]["lng"],
            )
        )
    if e.args["layerType"] == "polygon":
        m.generic_layer(name="polygon", args=[e.args["layer"]["_latlngs"]])
    if e.args["layerType"] == "rectangle":
        m.generic_layer(name="rectangle", args=[e.args["layer"]["_latlngs"]])
        # console.log(e.args['layer']['_latlngs'])
        print(e.args["layer"]["_latlngs"])


draw_control = {
    "draw": {
        "polygon": False,
        "marker": False,
        "circle": False,
        "rectangle": True,
        "polyline": False,
        "circlemarker": False,
    },
    "edit": False,
}
with ui.card().classes("w-full").style("height: 500px;"):
    m = ui.leaflet(
        center=(121.42835573711707, 25.00715235847364),
        zoom=13,
        draw_control=draw_control,
    ).style("height: 100%")
    m.on("draw:created", handle_draw)

ui.run()
